package com.inteligencia.stage.dataaccess.base.jdbc;

/**
 *
 * @author bicho
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JdbcConector {

    final String url = "jdbc:mysql://localhost/grid";
    final String dbdriver = "com.mysql.jdbc.Driver";
    final String username = "root";
    final String password = "sperma";
    //Connection Conec;
    Statement St;
    ResultSet reg;
    Connection Conec;

    /** Creando la instania de conexion */
    public JdbcConector() {
    }

    private Connection getConnection() {
        Connection Conec = null;
        try {
            Class.forName(dbdriver);
            Conec = DriverManager.getConnection(url, username, password);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }
        return Conec;
    }

    //Metodo de ejecucion de inserts,updates.deletes a la base de datos
    protected boolean ejecutar(String sql) {
        Conec = getConnection();
        try {
            St = Conec.createStatement();
            St.execute(sql);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    //Metodo para las consultas a la base de datos
    protected ResultSet Consultar(String sql) {
        Conec = getConnection();
        String error = "";
        try {
            St = Conec.createStatement();
            reg = St.executeQuery(sql);
        } catch (Exception ex) {
            error = ex.getMessage();
        }
        return (reg);
    }

    public void cerrar() {
        try {
            Conec.close();
        } catch (SQLException ex) {
            Logger.getLogger(JdbcConector.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
